package com.farm.farming.service.impl;

import com.alibaba.fastjson.JSON;
import com.farm.entity.PageResult;
import com.farm.farming.dao.CustomerMapper;
import com.farm.farming.pojo.Cust_menu;
import com.farm.farming.pojo.Customer;
import com.farm.farming.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.*;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public int add(Customer customer) {
        customerMapper.save(customer);
        return 1;
    }

    @Override
    public int delete(Integer id) {
        customerMapper.deleteById(id);
        return 1;
    }

    @Override
    public int update(Customer customer) {
        Map newMap = JSON.parseObject(JSON.toJSONString(customer),Map.class);
        //查询原来的数据
        Customer one = customerMapper.getOne(customer.getId());
        Map oldMap = JSON.parseObject(JSON.toJSONString(one),Map.class);

        Customer customer1 = this.copyColumnnullValue(newMap, oldMap);
        customerMapper.save(customer1);
        return 1;
    }

    private Customer copyColumnnullValue(Map newmap, Map oldmap){
        Set<Map.Entry<String,Object>> set = oldmap.entrySet();
        for (Map.Entry<String, Object> entry : set) {
            if (newmap.get(entry.getKey())==null){
                newmap.put(entry.getKey(),oldmap.get(entry.getKey()));
            }
        }
        Customer menu = JSON.parseObject(JSON.toJSONString(newmap), Customer.class);
        return menu;
    }


    @Override
    public List<Customer> findAll() {

        return customerMapper.findAll();
    }

    @Override
    public PageResult<Customer> findPage(Customer customer,Integer currentPage,Integer size) {
        PageRequest pageRequest = PageRequest.of(currentPage, size);

        /*if (customer==null)
            customer=new Customer();

        Example<Customer> example = Example.of(customer, ExampleMatcher.matching()
                .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("addr",ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("contacts",ExampleMatcher.GenericPropertyMatchers.contains()));
        //Example<Customer> example = Example.of(customer);
        //分页查询加条件
        org.springframework.data.domain.Page<Customer> customers = customerMapper.findAll(example,pageRequest);
        return new PageResult<Customer>(customers.getTotalElements(),customers.getContent());
*/
        Specification<Customer> specification = new Specification<Customer>() {
            @Override
            public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();
                if (customer.getActive_time()!=null) {
                    Predicate predicate1 = criteriaBuilder.between(root.get("active_time").as(Date.class), new Date(0), new Date());
                    list.add(predicate1);
                }
                if (customer.getName()!=null){
                    Predicate predicate2 = criteriaBuilder.like(root.get("name").as(String.class), "%" + (customer.getName()==null ? "":customer.getName()) + "%");
                    list.add(predicate2);
                }
                if (customer.getContacts()!=null){
                    Predicate predicate3 = criteriaBuilder.like(root.get("contacts").as(String.class), "%" + (customer.getContacts()==null ? "":customer.getContacts()) + "%");
                    list.add(predicate3);
                }
                if (customer.getAddr()!=null){
                    Predicate predicate4 = criteriaBuilder.like(root.get("addr").as(String.class), "%" + (customer.getAddr()==null ? "":customer.getAddr()) + "%");
                    list.add(predicate4);
                }
                Predicate[] pre = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(pre));
            }
        };

        Page<Customer> page = customerMapper.findAll(specification, pageRequest);
        return new PageResult<>(page.getTotalElements(),page.getContent());
    }

    @Override
    public Customer findById(Integer id) {

        return customerMapper.getOne(id);
    }

}
